<?php
/**
 * 查询构建器 find() select()
 * ORM get() all()
 */

namespace app\api\model;


use app\api\model\Category as CategoryModel;

class Category extends BaseModel
{
    protected $hidden = ['create_time', 'update_time', 'delete_time', 'topic_img_id'];

//    public function products()
//    {
//        return $this->hasMany('Product', 'category_id', 'id');
//    }

    public function topicImg()
    {
        return $this->belongsTo('Image', 'topic_img_id', 'id');
    }

    public function children()
    {
        return $this->hasMany('Category', 'parent_id', 'id');
    }

    public static function getOne($id)
    {
        return self::with(['topicImg', 'children', 'children.topicImg'])->find($id);
    }

    public static function getRawOne($id)
    {
        //return self::with(['topicImg'])->where('parent_id', '=', $id)->select();
        return self::with(['topicImg'])->where('id', '=', $id)->select();
    }

    public static function getRawChildren($id)
    {
        //return self::with(['topicImg'])->where('parent_id', '=', $id)->select();
        return self::with(['topicImg'])->where('parent_id', '=', $id)->select();
    }
    /**
     * 查询构建器 find() select()
     * ORM get() all()
     */
    public static function getAll($page, $size)
    {
//        return self::all(['parent_id' => 0], ['topicImg', 'children', 'children.topicImg']);
        return self::with(['topicImg', 'children', 'children.topicImg'])->where('parent_id', '=',0)->paginate($size, true, ['page' => $page]);
    }

    public static function getRawAll($page, $size){
        return self::with('topicImg')->order('id desc')->paginate($size, true, ['page'=>$page]);
    }

    public function getImgAttr($value, $data)
    {
        return $this->img_prefix($value, $data);
    }
}